//
//  BigHeadView.m
//  7.13苏宁框架
//
//  Created by mini on 15/7/19.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "BigHeadView.h"
#import "bigHeadModel.h"
#import "Common.h"
#import "bigHeadViewCell.h"
#define totalSection 100
static NSString *const name = @"bigCell";
@implementation BigHeadView

//-(instancetype)init
//{
//    self = [super init];
//    if (self) {
//        self = [[[NSBundle mainBundle]loadNibNamed:@"BbigHeadView" owner:nil options:nil]lastObject];
//    }
//    return self;
//}
+(id)createHead
{


        return [[[NSBundle mainBundle]loadNibNamed:@"BbigHeadView" owner:nil options:nil]lastObject];

}

-(void)awakeFromNib
{
    //因为画的所以默认流式布局
    // 注册单元格
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.itemSize = CGSizeMake(320, 107);
    flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0;
    self.collectionView.collectionViewLayout = flow;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self.collectionView registerNib:[UINib nibWithNibName:@"bigHeadViewCell" bundle:nil] forCellWithReuseIdentifier:name];
    //使表滚动到第一行第一个
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.contentSize = CGSizeMake(320*100, 0);
    //保证collectionView在最底下
    [self sendSubviewToBack:self.collectionView];
    //添加计时器
    [self addTimer];
}

-(void)addTimer
{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(imageChange) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer;
}

-(NSArray*)total
{
    _total = [NSArray array];
    _total = [bigHeadModel modelArrayWithFilename:@"bigHead.plist"];
    return _total;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 100;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.total.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    bigHeadViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:name forIndexPath:indexPath];
    [cell setModel:_total[indexPath.item]];
    
    return cell;
}

-(NSIndexPath*)current
{
    //当前位置
    NSIndexPath *path = [[self.collectionView indexPathsForVisibleItems]lastObject];
    //中间段跟当前一样的位置
    NSIndexPath *centerPath = [NSIndexPath indexPathForItem:path.item inSection:totalSection/2];
    //滚动到中间
    [self.collectionView scrollToItemAtIndexPath:centerPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    //返回位置
    return centerPath;
    
}
-(void)imageChange
{
    NSIndexPath *currentPath = [self current];
    //计算下一个
    NSInteger nextItem = currentPath.item + 1;
    NSUInteger nextSection = currentPath.section;
    //判断当前是不是最后一个,是的话换到下一段第一个
    if (nextItem == _total.count) {
        nextItem = 0;
        nextSection++;
    }
    //滚动到下一个
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:nextItem inSection:nextSection] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
        self.pageView.currentPage = nextItem;
}
#pragma mark  - UICollectionViewDelegate
/**
 *  当用户即将开始拖拽的时候就调用
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    _timer = nil;
}

/**
 *  当用户停止拖拽的时候就调用
 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    int index = (int)((scrollView.contentOffset.x-160)/320+1)%4;

    self.pageView.currentPage = index;
    
     [self addTimer];
//    int index = (int )(scrollView.contentOffset.x /320)%4;
//
//
    
    
}
@end
